package com.baizhi.service.impl;

import com.baizhi.dao.AdminDao;
import com.baizhi.dto.LoginDTO;
import com.baizhi.entity.Admin;
import com.baizhi.po.AdminPO;
import com.baizhi.service.AdminService;
import com.baizhi.util.TokenUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;

/**
 * (Admin)表服务实现类
 *
 * @author makejava
 * @since 2021-09-23 19:30:04
 */
@Service("adminService")
@Transactional
public class AdminServiceImpl extends ServiceImpl<AdminDao, Admin> implements AdminService {

    @Autowired
    private AdminDao adminDao;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;  //对字符串支持比较友好,不能存储对象
    @Autowired
    private RedisTemplate redisTemplate;  //存储对象

    /**
     * 根据有账号密码查询
     *
     * @param loginDTO
     */
    @Override
    public HashMap<String, Object> queryByUserNameAndPassword(LoginDTO loginDTO) {
        HashMap<String, Object> map = new HashMap<>();
        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Admin::getUsername, loginDTO.getUsername());
        Admin admin = adminDao.selectOne(queryWrapper);
        if (admin != null) {
            String password = admin.getPassword();
            if (password.equals(loginDTO.getPassword())) {
                //根据用户id生成token
                String sign = TokenUtils.sign(admin.getId().longValue());
                //将token作为键传入redis数据库中，键为token，值为对象，可以存储为map格式
                String username = admin.getUsername();
                String avatar = admin.getAvatar();
                HashOperations<String, Object, Object> hash = stringRedisTemplate.opsForHash();
                hash.put(sign, "username", username);
                hash.put(sign, "avatar", avatar);
                String username1 = hash.get(sign, "username").toString();
                String avatar1 = hash.get(sign, "avatar").toString();
                System.out.println("username1 = " + username1);
                System.out.println("avatar1 = " + avatar1);

                //返回数据
                map.put("status", 200);
                map.put("token", sign);
            } else {
                //密码错误
                map.put("status", 403);
                map.put("message", "账号或密码不正确");
            }
        } else {
            //登录错误
            map.put("status", 404);
            map.put("message", "账号不存在");
        }
        return map;
    }

    @Override
    public void deleteToken(String token) {
        //到redis数据库中删除token对应的数据
        Boolean delete = stringRedisTemplate.delete(token);
        System.out.println("delete = " + delete);
    }

    @Override
    public AdminPO query(String token) {
        //根据token获取对应的数据
        HashOperations<String, Object, Object> hash = stringRedisTemplate.opsForHash();
        String username = hash.get(token, "username").toString();
        String avatar = hash.get(token, "avatar").toString();
        AdminPO adminPO = new AdminPO(username, avatar);
        return adminPO;
    }
}
